<template>
  <div class="login-container">
    <div class="login-wrap">
      <div class="loginleft"><img src="../../assets/login/banner.jpg" class="loginbg">
        <span class="loginlogo">
          <svg-icon icon-class="jxl_logo" />
        </span>
        <div class="systemname">卫生经济运营高质量发展大数据监管平台<img src="../../assets/login/hrp_1.png" height="30">
          <p>HOSPITAL OPERATIONAL MANAGEMENT PLATFORM</p>
        </div>
      </div>
      <div class="logintext">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <div class="title-container">
            <h3 class="title">
              欢迎登录
              <i />
            </h3>
          </div>
          <el-form-item prop="username" :class="{ 'inputcur' : focusName}">
            <span class="svg-container">
              <svg-icon icon-class="user2" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="账号"
              name="username"
              type="text"
              auto-complete="on"
              @blur="focusName=false"
              @focus="focusName=true"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password" :class="{ 'inputcur' : focusPassword}">
              <span class="svg-container">
                <svg-icon icon-class="password2" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                auto-complete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false;focusPassword=false"
                @focus="focusPassword=true"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
          <el-button :loading="loading" type="primary" class="btn-style" @click.native.prevent="handleLogin">
            {{ '登录' }}
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      focusName: false,
      focusPassword: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
  },
  methods: {
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('登录错误！')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#333;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  padding: 50px;
  height: 100vh;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      font-size:20px;
      color:#666;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-form-item {
    border-bottom: 1px solid #bbbbbb;
    background: #fff;
    color: #666;
    font-size:20px;
    margin-bottom: 40px
  }
  .el-form-item.inputcur{
    border-bottom: 1px solid #07a5a6;
    .svg-container{color:#07a5a6}
    input {color:#07a5a6}
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url("../../assets/login/bg.jpg") no-repeat;
  background-size:cover;
  overflow: hidden;
.login-wrap{
    background:#fff;
    width:100%;
    height:100%;
    display: flex;flex-direction: row;justify-content:center;
    .loginleft {flex-grow: 0; height: 100%; position: relative;
      .loginbg{height: 100%;}
      .loginlogo {
        position: absolute;
        top: 20px;
        left: 44px;
        width:100px;
        .svg-icon{
          width:5rem;
          height:5rem
        }
      }
      .systemname {position: absolute;bottom: 10px; right: 50px;color:#fff;font-size:28px;letter-spacing: 3px;
        p{font-size: 13px;letter-spacing:0px}
        img {vertical-align: middle;margin-left:10px}
      }
    }
    .logintext{height:100%;width:100%; text-align:center;display: flex;align-items:center;}
  }
  .btn-style {
    width:100%;
    height:60px;
    background:#07a5a6;
    letter-spacing: 5px;
    border-radius:5px;
    font-size: 20px;
    text-align: center;
    margin-top:20px
    }
    .btn-style:active{
       box-shadow:0px 6px 6px 0px rgba(10,166,167,0.3);
    }
  .login-form {
    position: relative;
    width: 60%;
    margin: 0 auto;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    display: inline-block;
    font-size:24px;
  }

  .title-container {
    position: relative;
    font-size: 0;margin-bottom:60px;
    .title {
      font-size: 30px;
      color: #07a5a6;
      font-weight: bold;
      display:inline-block;
      margin:0;padding:0;
      i{border-bottom:5px solid #07a5a6; height: 5px; display: block; margin-top:28px;border-radius:3px;}
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
